using UnityEngine;
using System.Collections;

public class TopicManager : MonoBehaviour {

    public int topicQuantity;
    public Material[] clickedMaterial;
    public Material[] firstMaterial;
    public GameObject[] topics;

	void Start () 
    {
        topics = new GameObject[topicQuantity];
        firstMaterial = new Material[topicQuantity];

        for (int i = 0; i < topicQuantity; i++)
        {
            topics[i] = GameObject.Find("Topic_" + i.ToString());
            print("Add Topic_" + i.ToString());
            firstMaterial[i] = topics[i].renderer.material;
            print("Add Topic Material: " + i);
        }
	}
	
	void Update () 
    {
        for (int i = 0; i < Input.touches.Length; i++)
        {
            Touch touch = Input.touches[0];

            Ray ray = Camera.main.ScreenPointToRay(touch.position);
            RaycastHit hit;

            if (Input.touchCount >= 0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.transform.gameObject.tag == "Topic")
                    {
                        string[] a = hit.transform.name.Split('_');
                        hit.transform.renderer.material = clickedMaterial[int.Parse(a[1])];
                   }
                }
            }

            if (Input.touchCount >= 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
            {
                if (Physics.Raycast(ray, out hit))
                {
                    if (hit.transform.gameObject.tag == "Topic")
                    {
                        string[] a = hit.transform.name.Split('_');
                        hit.transform.renderer.material = clickedMaterial[int.Parse(a[1])];
                        Application.LoadLevel(hit.transform.name);
                    }
                }
            }
        }
	}
}
